<?php 
namespace app\common\controller;


class Base64
{
	public function up_base64($base64,$address)
	{
		$address = $address.date('Y/m/d').'/';
		if(!is_dir($address)){
			mkdir($address,0777,true);
		}
		if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64,$result)) {
			$ext = $result[2];
			$name = date('ymdHis') . rand(0, 1000);
			$filurl = $address."{$name}.{$ext}";
			if (file_put_contents($filurl, base64_decode(str_replace($result[1], '', $base64)))) {
				$url = substr($filurl,1);
				return $url;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	public function uniAppUpload($address,$file)
	{
		//获取扩展名
		$exename  = $this->getExeName($file['name']);
		if($exename != 'png' && $exename != 'jpg' && $exename != 'gif'){
			exit('不允许的扩展名');
		}
		$address = $address.date('Y/m/d').'/';
		if(!is_dir($address)){
			mkdir($address,0777,true);
		}
		$imageSavePath = $address.time().'.'.$exename;
		if(move_uploaded_file($file['tmp_name'], $imageSavePath)){
			$url = substr($imageSavePath,1);
			return $url;
		}else{
			return false;
		}
	}
	public function getExeName($fileName){
		$pathinfo      = pathinfo($fileName);
		return strtolower($pathinfo['extension']);
	}
}